package com.gcy.checkcertificate.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;
import java.util.List;

/**
 * <Description> <br>
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2024/01/26 <br>
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Value("${spring.application.name}")
    private String applicationName;
    /**
     * 默认开启swagger文档
     */
    @Value("${swagger.enable:true}")
    private boolean swaggerEnable;

    @Value("${swagger.basePackage:com.gcy}")
    private String basePackage;

    @Bean
    public Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo())
                .enable(swaggerEnable)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any()).build()
//                .enableUrlTemplating(true)//解决映射同URL不同params只映射成一个问题
//                .useDefaultResponseMessages(false)//删除swagger中的操作的响应体
                .globalOperationParameters(globalOperationParameters());
    }

    @Bean
    @Conditional(GroupCondition.class)
    public Docket groupApi(Environment environment) {
        String groupName = environment.getProperty("swagger.group.name");
        String groupPathRegex = environment.getProperty("swagger.group.pathRegex");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .enable(swaggerEnable)
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.ant(groupPathRegex)).build()
//                .enableUrlTemplating(true)//解决映射同URL不同params只映射成一个问题
//                .useDefaultResponseMessages(false)//删除swagger中的操作的响应体
                .globalOperationParameters(globalOperationParameters());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(applicationName)
                .description("# " + applicationName + "接口文档") //描述字段支持Markdown语法
                .version("1.0.0").build();
    }

    private List<Parameter> globalOperationParameters() {
        return Collections.singletonList(new ParameterBuilder().name("token").description("调用凭证").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
    }
}
